package com.cdks.wuxiu.api.service.mysql;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdks.wuxiu.api.component.constant.Constant;
import com.cdks.wuxiu.api.mapper.UserBillInfoMapper;
import com.cdks.wuxiu.api.util.TimeUtils;
import com.cdks.wuxiu.entity.mysql.UserBillInfo;

import java.util.List;

import org.springframework.stereotype.Service;

/**
 * 用户账单 服务实现类
 */
@Service
public class UserBillInfoServiceImpl extends ServiceImpl<UserBillInfoMapper, UserBillInfo> implements UserBillInfoService {
    @Override
    public List<UserBillInfo> getByUserId(long userId, int page, long start, long stop) {
        QueryWrapper<UserBillInfo> wrapper  = new QueryWrapper<>();
        wrapper.lambda().ge(UserBillInfo::getCreateTime, start)
                        .lt(UserBillInfo::getCreateTime, stop)
                        .select(UserBillInfo::getId,
                                UserBillInfo::getCreateTime,
                                UserBillInfo::getPayerId,
                                UserBillInfo::getEarnerId,
                                UserBillInfo::getPayReal,
                                UserBillInfo::getEarnReal,
                                UserBillInfo::getPayType,
                                UserBillInfo::getEarnType,
                                UserBillInfo::getRemark);
        wrapper.last("AND (((payerId=" + userId + ") AND (payReal > 0)) OR ((earnerId=" + userId + ") AND (EarnReal > 0))) ORDER BY createTime DESC LIMIT " + page * Constant.LIST_PAGE_SIZE + "," + Constant.LIST_PAGE_SIZE);

        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<UserBillInfo> getByUserIdOnlyScore(long userId, int page, long start, long stop) {
        QueryWrapper<UserBillInfo> wrapper  = new QueryWrapper<>();
        wrapper.lambda().ge(UserBillInfo::getCreateTime, start)
                        .lt(UserBillInfo::getCreateTime, stop)
                        .gt(UserBillInfo::getPayScore, 0)
                        .select(UserBillInfo::getCreateTime,
                                UserBillInfo::getPayerId,
                                UserBillInfo::getEarnerId,
                                UserBillInfo::getPayScore,
                                UserBillInfo::getRemark);
        wrapper.last("AND (((payerId=" + userId + ") AND (payScore > 0)) OR ((earnerId=" + userId + ") AND (EarnReal > 0) AND (earnType = 3))) ORDER BY createTime DESC LIMIT " + page * Constant.LIST_PAGE_SIZE + "," + Constant.LIST_PAGE_SIZE);
        
        return baseMapper.selectList(wrapper);
    }

    @Override
    public int saveBill(long payBill, long earnBill, long payReal, long payScore, long earnReal, long payerId, long earnerId, int payType, int earnType, long traceId, String remark) {
        UserBillInfo bill  = new UserBillInfo();
        bill.setCreateTime(TimeUtils.getTimeSecond());
        bill.setPayBill((int) payBill);
        bill.setEarnBill((int) earnBill);
        bill.setPayReal((int) payReal);
        bill.setPayScore((int) payScore);
        bill.setEarnReal((int) earnReal);
        bill.setPayType(payType);
        bill.setEarnType(earnType);
        bill.setPayerId(payerId);
        bill.setEarnerId(earnerId);
        bill.setTraceId(traceId);
        bill.setRemark(remark);

        int ret  = baseMapper.insert(bill);
        if (ret > 0)
        {
            return 0;
        }

        log.error("bill save failed: " + bill);

        return -1;
    }
}
